ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
'< -----------------End of function-----------------Start Script----------------- >'
GetRequestDetails : winstr = Split(GetC1Env(UserDetail(0)), "cis.jsp", -1, 1)(0) & "uiPage"
FindC1EnvInIE(GetC1Env(UserDetail(0))) : validUser(UserDetail(2))
'<----------------------------Get Model User  Portals Profile User ID
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
while TabPageContent Is Nothing
	WScript.Sleep 200 : Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
wend
Set ModelUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
if ModelUserPPUI.value ="" then
	ModelUserPPUI=""
else
	ModelUserPPUI=ModelUserPPUI.value
end if
'<---------------------Locate the  Group Part'
Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
Set TabMenuContent = TabMenu.contentWindow
Set TDList = TabMenuContent.document.getelementsbytagname("td")
Set userGroupGridContent = TabPageContent.document.getelementbyid("userGroupGrid").contentWindow
Set Groups= userGroupGridContent.document.getelementsbytagname("input")
set fso = createobject("scripting.filesystemobject")
set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0)& "_Groups.txt", true)
for each item in Groups
	if item.classname = "data dc-CHAR-NONE-20-0 forms" and Len(item.value ) <> 0 then
		ReDim Preserve ModelUserGroupList(ModelUserGroupNum)
		ModelUserGroupList(ModelUserGroupNum) = item.value
		file.WriteLine(ModelUserGroupList(ModelUserGroupNum))
		ModelUserGroupNum =ModelUserGroupNum +1
	end if
Next
for each item in Groups
	if item.classname = "data dc-DATE-NONE-10-0 forms" and Len(item.value ) <> 0 then 
		ReDim Preserve ModelUserGroupExpList(ModelUserGroupExpNum)
		ModelUserGroupExpList(ModelUserGroupExpNum) = item.value
		ModelUserGroupExpNum=ModelUserGroupExpNum+1
	end if
Next
file.Close
'<----------------------Switch to TODO Roles'
set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0)& "_Todo.txt", true)
SwitchTab("To Do Roles") :  GetMoreVlues("userRoleGrid")'<------------find if there is more todo roles'
'<----------get todo roles list'
Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
Set ToDoRoles= userRoleGrid.contentWindow.document.getelementsbytagname("input")
for each item in ToDoRoles
	if item.value = "" then exit for
	ReDim Preserve ModelUserTodoList(ModelUserTodoNum)
	ModelUserTodoList(ModelUserTodoNum) = item.value
	file.WriteLine(ModelUserTodoList(ModelUserTodoNum))
	ModelUserTodoNum =ModelUserTodoNum +1
Next
file.Close
'<--------------------------------Switch to Characteristics''
set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Sales.txt", true)
SwitchTab("Characteristics")
'<---------------------Locate the Characteristics '
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set userCharGridContent = TabPageContent.document.getelementbyid("USER_CHAR_GRID").contentWindow
Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
on error resume next
while Characteristics Is Nothing
	WScript.Sleep 200
	Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
wend
on error goto 0
for each item in Characteristics
	if item.value = "SALCHN" then
		if ModelUserSaleNum < 10 then
			saleValueID = "USER_CHAR:" & Mid(item.id,11,1) & "$CHAR_VAL"
		else
			saleValueID = "USER_CHAR:" & Mid(item.id,11,2) & "$CHAR_VAL"
		end if
		ReDim Preserve ModelUserSaleList(ModelUserSaleNum)
		ModelUserSaleList(ModelUserSaleNum) = userCharGridContent.document.getelementbyid(saleValueID).value
		file.WriteLine(ModelUserSaleList(ModelUserSaleNum))
		ModelUserSaleNum=ModelUserSaleNum+1
	end if
Next

file.Close : set file = nothing : set fso = nothing
'###########   1 Add sales chennel   ##############'
validUser(UserDetail(1))
if isErrorIDFound = "true"  then
	msgbox("the  ID doesn't exist or has multiple matched value")
	WScript.quit
end if
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
if ModelUserSaleList(0) <> "NoDataHere" then
	const C_Type = "SALCHN"
	Set userCharGridContent = TabPageContent.document.getelementbyid("USER_CHAR_GRID").contentWindow
'<------------------------Add end user sale channel'
	GetEndUserSaleList
	AddSalesList EndUserSaleList,ModelUserSaleList
end if
'###########   2 Todo Roles   ##############'
if ModelUserTodoNum <> 0 then
	SwitchTab("To Do Roles") :  GetMoreVlues("userRoleGrid")
	Set userRoleGridContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid").contentWindow
	Set OldTDRoles= userRoleGridContent.document.getelementsbytagname("input")
	dim EndUserTodoListNum
	for each item in OldTDRoles ' <--------------get end user todo roles
		if item.value = "" then exit for
		EndUserTodoList = item.value & ";" & EndUserTodoList
		EndUserTodoListNum=EndUserTodoListNum+1
	Next
	Set TodoRoleAdd= userRoleGridContent.document.getelementbyid("IM_USER_ROLE:0$FirstSec_addBut")
	'<-----------------Add ToDo Roles
	if IsEmpty(EndUserTodoList)  then 'end user doesn't have any TODO role previously'
		for num = 1 to  UBound (ModelUserTodoList)
			if num =49 then exit for
			lastnum = num+1 : TodoRoleAdd.click
		next
		WScript.Sleep 1000 
		for num = 0 to  UBound (ModelUserTodoList)
			if num =49 then exit for
			Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
			Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:" & num & "$ROLE_ID")
			TodoInfo.value =ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
			do while userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:" & num & "$DESCR").innerHTML="" : WScript.Sleep 100 : Loop
		next	
	else 'end user already has TODO role, need to exclude duplication 
		dim fnum,TodoInfoID : lastnum = EndUserTodoListNum-1 : fnum = lastnum
		Set regEx = New RegExp
		for num = 0 to UBound(ModelUserTodoList)
			if lastnum-fnum >49 then 
				lastnum=lastnum-fnum : exit for
			end if
			str=ModelUserTodoList(num) & ";" : regEx.Pattern = str
			regEx.Global = True : res = regEx.Test(EndUserTodoList) : TodoInfoID = lastnum+1
			if res = "False" then
				Set TodoRoleAdd= userRoleGrid.contentWindow.document.getelementbyid("IM_USER_ROLE:" & lastnum &  "$FirstSec_addBut") : TodoRoleAdd.click
				Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
				while TodoInfo Is Nothing
					WScript.Sleep 100
					Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
				wend
				TodoInfo.value = ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
				do while userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID &"$DESCR").innerHTML="" : WScript.Sleep 100: Loop
				lastnum=lastnum+1
			end if
		next
	end if
end if
'###########   3 Add Groups   ##############'
SwitchTab("Main")
Set TabPage = ie.document.frames(0).document.getelementbyid("tabPage")
while TabPage Is Nothing
	WScript.Sleep 200
	Set TabPage = ie.document.frames(0).document.getelementbyid("tabPage")
wend
Set TabPageContent = TabPage.contentWindow
if ModelUserGroupNum <> 0 then
	Set userGroupGrid = TabPageContent.document.getelementbyid("userGroupGrid")
	Set userGroupGridContent = userGroupGrid.contentWindow
	Set GroupAddBut= userGroupGridContent.document.getelementbyid("IM_USER_USR_GRP:0$FirstSec_addBut")
	Set OldGroups= userGroupGridContent.document.getelementsbytagname("input")
	for each item in OldGroups '<----get end user groups to remove duplicate ones
		dim EndUserGroupListNum
		if item.classname = "data dc-CHAR-NONE-20-0 forms" and Len(item.value ) <> 0 then
			EndUserGroupList =  item.value & ";" & EndUserGroupList
			EndUserGroupListNum=EndUserGroupListNum+1
		end if
	Next
	'<-----------------Add Groups'
	if IsEmpty(EndUserGroupList)  then 'end user doesn't have any group previously'
		for num = 1 to UBound (ModelUserGroupList)
			GroupAddBut.click 
		next
		for num = 0 to UBound (ModelUserGroupList)
			Set GrpInfo = userGroupGridContent.document.getelementbyid("USER_USR_GRP:" & num & "$USR_GRP_ID")
			GrpInfo.value = ModelUserGroupList(num) : GrpInfo.FireEvent("onblur")
			Set GrpExp = userGroupGridContent.document.getelementbyid("USER_USR_GRP:" & num & "$EXPIRATION_DT")
			GrpExp.value =ModelUserGroupExpList(num): GrpExp.FireEvent("onblur")
			do while userGroupGridContent.document.getelementbyid("USER_USR_GRP:"& num & "$DESCR").innerHTML="" : WScript.Sleep 100: Loop
		next
	else 'end user already has group, need to exclude duplication '
		Set regEx = New RegExp
		for num = 0 to UBound(ModelUserGroupList)
			str=ModelUserGroupList(num) & ";"
			regEx.Pattern = str : regEx.Global = True : res = regEx.Test(EndUserGroupList)
			if res = "False" then 'add directly'
				dim nextGrp,actualNum : actualNum = EndUserGroupListNum-1 :nextGrp=actualNum+1
				Set GroupAddBut = userGroupGridContent.document.getelementbyid("IM_USER_USR_GRP:"& actualNum & "$FirstSec_addBut") : 	GroupAddBut.click
				Set GrpInfo = userGroupGridContent.document.getelementbyid("USER_USR_GRP:" & nextGrp & "$USR_GRP_ID")
				while GrpInfo Is Nothing 
					WScript.Sleep 100
					Set GrpInfo = userGroupGridContent.document.getelementbyid("USER_USR_GRP:" & nextGrp & "$USR_GRP_ID")
				wend
				GrpInfo.value = ModelUserGroupList(num) : GrpInfo.FireEvent("onblur")
				do while userGroupGridContent.document.getelementbyid("USER_USR_GRP:"& nextGrp & "$DESCR").innerHTML="" : WScript.Sleep 100: Loop
				Set GrpExp = userGroupGridContent.document.getelementbyid("USER_USR_GRP:" & nextGrp & "$EXPIRATION_DT")
				GrpExp.value =ModelUserGroupExpList(num): GrpExp.FireEvent("onblur")
				actualNum=actualNum+1
			else 'already has duplicate group, only change the expire date'
				for each grp in OldGroups
					if grp.value = ModelUserGroupList(num) then
						set grpexp = userGroupGridContent.document.getelementbyid(Split(grp, "$", -1, 1)(0) & "$EXPIRATION_DT")
						grpexp.value = ModelUserGroupExpList(num) : grpexp.FireEvent("onblur") 
						do while userGroupGridContent.document.getelementbyid(Split(grp, "$", -1, 1)(0) & "$OWNER_FLG").innerHTML="" : WScript.Sleep 100: Loop
					exit for
					end if
				next
			end if
		next
	end if
end if
REM '###########   4 Update End User Portals Profile User ID  ##############'
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set EndUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
Set UserTypeFlg = TabPageContent.document.getelementbyid("USER_TYPE_FLG")
ie.Visible=true
if ModelUserPPUI <> "" and ModelUserPPUI <> "PORTAL" then
	UserTypeFlg.value="TMPL" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	UserTypeFlg.value="" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500	
	EndUserPPUI.value=ModelUserPPUI: EndUserPPUI.FireEvent("onblur") : WScript.Sleep 1500
	ie.document.frames(0).document.getelementbyid("IM_SAVE").click
elseif EndUserPPUI.value = "PORTAL" then
	UserTypeFlg.value="TMPL" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	UserTypeFlg.value="" : UserTypeFlg.FireEvent("onchange"): WScript.Sleep 1500
	ie.document.frames(0).document.getelementbyid("IM_SAVE").click
end if
ie.document.frames(0).document.getelementbyid("IM_SAVE").click
msgbox "Script finished successfully!"
